import React from 'react';
import { useDispatch } from 'react-redux';
import { useSelector } from 'react-redux';
import { toggleTodoCreator, delTodoCreator } from '../store/action/todo';

export default function Main() {
  const list = useSelector((state) => state.todo.list);
  const dispatch = useDispatch();

  return (
    <section className="main">
      <input id="toggle-all" className="toggle-all" type="checkbox" />
      <label htmlFor="toggle-all">全选</label>
      <ul className="todo-list">
        {list.map((item) => (
          <li key={item.id} className={item.isDone ? 'completed' : ''}>
            <div className="view">
              <input
                className="toggle"
                type="checkbox"
                checked={item.checked}
                onChange={() => dispatch(toggleTodoCreator(item.id))}
              />
              <label>{item.name}</label>

              <button
                className="destroy"
                onClick={() => dispatch(delTodoCreator(item.id))}
              ></button>
            </div>
          </li>
        ))}
      </ul>
    </section>
  );
}
